package com.leyou.auth.config;

import com.leyou.auth.interceptor.LoginInterceptor;
import com.leyou.auth.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;


@Configuration
@ConditionalOnProperty(prefix = "ly.auth", name = "enable", havingValue = "true")
public class MvConfig implements WebMvcConfigurer {

    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private ClientProperties properties;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册一个拦截器
        InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor(jwtUtils));
        //添加拦截路径
        List<String> includePathPatterns = properties.getIncludePathPatterns();
        if (!CollectionUtils.isEmpty(includePathPatterns)){
            registration.addPathPatterns(includePathPatterns);
        }

        //添加不拦截路径
        List<String> excludePathPatterns = properties.getExcludePathPatterns();
        if (!CollectionUtils.isEmpty(excludePathPatterns)){
            registration.excludePathPatterns(excludePathPatterns);
        }
    }
}
